package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.MemberEntity;
import com.hy.admin.helper.request.AccountRequest;
import com.hy.admin.helper.request.MemberRequest;
import com.hy.admin.helper.request.ResetPasswordRequest;
import com.hy.admin.service.AccountService;
import com.hy.admin.service.MemberService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import com.hy.utils.SmsDemo;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by shitou on 17/12/26.
 */
@RestController
@RequestMapping("/api/member")
public class MemberController {

    protected final Logger logBase = LoggerFactory.getLogger(MemberController.class);
    @Autowired
    private AccountService accountService;
    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/init", method = RequestMethod.POST)
    public ApiResponseDTO init(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("init_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        MemberRequest memberRequest = gson.fromJson(apiRequestDTO.getRequestData(), MemberRequest.class);
        ServicesResult<MemberEntity> accountResponse=null;
        accountResponse = accountService.init(memberRequest.getDeviceIdentify());
        apiResponseDTO.initServicesResult(accountResponse);
        apiResponseDTO.setResponseData(gson.toJson(accountResponse.getResult()));
        logBase.info("init_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ApiResponseDTO Register(@RequestBody final ApiRequestDTO apiRequestDTO,HttpServletRequest request) {
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("register_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        AccountRequest accountRequest = gson.fromJson(apiRequestDTO.getRequestData(), AccountRequest.class);
        //验证手机验证码 TODO 后期代码优化
        Object curSmsCode= SmsDemo.getCode(accountRequest.getMobile());
        ServicesResult<MemberEntity> accountResponse=null;
        if(curSmsCode!=null&&curSmsCode.toString().equals(accountRequest.getSmsVerCode())){
            accountResponse = accountService.Register(accountRequest);
            SmsDemo.removeCode(accountRequest.getMobile());
        }else {
            accountResponse=new ServicesResult<>();

            accountResponse.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            accountResponse.setStatusMsg("验证码错误");
        }
        apiResponseDTO.initServicesResult(accountResponse);
        apiResponseDTO.setResponseData(gson.toJson(accountResponse.getResult()));
        logBase.info("register_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponseDTO Update(ApiRequestDTO apiRequestDTO, @RequestParam(required=false,value="files") MultipartFile file){
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("update_request:{}", gson.toJson(apiRequestDTO));
        logBase.info("files:isEmpty-----" + (file==null));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        MemberRequest memberRequest= gson.fromJson(apiRequestDTO.getRequestData(), MemberRequest.class);
        ServicesResult<Boolean> servicesResult= null;
        try {
            servicesResult = memberService.Update(memberRequest, file);
        } catch (Exception e) {
            e.printStackTrace();
            servicesResult = new ServicesResult<>();
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(servicesResult);
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("update_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 上传教师证
     * @param apiRequestDTO
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public ApiResponseDTO upload(ApiRequestDTO apiRequestDTO, @RequestParam("files") MultipartFile file){
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("upload_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        MemberRequest memberRequest= gson.fromJson(apiRequestDTO.getRequestData(), MemberRequest.class);
        ServicesResult<Boolean> servicesResult= null;
        try {
            servicesResult = memberService.upload(memberRequest.getId(), file);
        } catch (IOException e) {
            e.printStackTrace();
            servicesResult = new ServicesResult<>();
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(servicesResult);
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("upload_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    @RequestMapping(value = "/isExist", method = RequestMethod.POST)
    public ApiResponseDTO IsExist(@RequestBody final ApiRequestDTO apiRequestDTO){
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("isExist_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        AccountRequest accountRequest = gson.fromJson(apiRequestDTO.getRequestData(), AccountRequest.class);
        ServicesResult<Boolean> accountResponse =memberService.IsExist(accountRequest);
        apiResponseDTO.initServicesResult(accountResponse);
        apiResponseDTO.setResponseData(gson.toJson(accountResponse.getResult()));
        logBase.info("isExist_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    @RequestMapping(value = "/editPwd", method = RequestMethod.POST)
    public ApiResponseDTO updatePwd(@RequestBody final ApiRequestDTO apiRequestDTO){
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("editPwd_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        AccountRequest accountRequest = gson.fromJson(apiRequestDTO.getRequestData(), AccountRequest.class);

        Object curSmsCode= SmsDemo.getCode(accountRequest.getMobile());
        ServicesResult<Boolean> accountResponse=null;
        if(curSmsCode!=null&&curSmsCode.toString().equals(accountRequest.getSmsVerCode())){
            accountResponse = accountService.updatePwd(accountRequest.getMobile(), accountRequest.getPassword());
            SmsDemo.removeCode(accountRequest.getMobile());
        }else {
            accountResponse = new ServicesResult<>();
            accountResponse.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            accountResponse.setStatusMsg("验证码错误");
        }
        apiResponseDTO.initServicesResult(accountResponse);
        apiResponseDTO.setResponseData(gson.toJson(accountResponse.getResult()));
        logBase.info("editPwd_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public ApiResponseDTO resetPwd(@RequestBody final ApiRequestDTO apiRequestDTO){
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("editPwd_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        ResetPasswordRequest resetPasswordRequest = gson.fromJson(apiRequestDTO.getRequestData(), ResetPasswordRequest.class);

        ServicesResult<Boolean> accountResponse = accountService.resetPwd(resetPasswordRequest.getOldPassword(),
                resetPasswordRequest.getMobile(), resetPasswordRequest.getNewPassword());
        apiResponseDTO.initServicesResult(accountResponse);
        apiResponseDTO.setResponseData(gson.toJson(accountResponse.getResult()));
        logBase.info("editPwd_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    @RequestMapping(value = "/updToken", method = RequestMethod.POST)
    public ApiResponseDTO updDeviceToken(@RequestBody final ApiRequestDTO apiRequestDTO){
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("updToken_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        MemberRequest memberRequest = gson.fromJson(apiRequestDTO.getRequestData(), MemberRequest.class);

        ServicesResult<Boolean> memberResponse = memberService.updDeviceToken(memberRequest.getId(),memberRequest.getDeviceToken());
        apiResponseDTO.initServicesResult(memberResponse);
        apiResponseDTO.setResponseData(gson.toJson(memberResponse.getResult()));
        logBase.info("updToken_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    @RequestMapping(value = "/updDeviceId", method = RequestMethod.POST)
    public ApiResponseDTO updDeviceId(@RequestBody final ApiRequestDTO apiRequestDTO){
        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        logBase.info("updDeviceId_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());
        MemberRequest memberRequest = gson.fromJson(apiRequestDTO.getRequestData(), MemberRequest.class);

        ServicesResult<Boolean> memberResponse = memberService.updAliDeviceId(memberRequest.getId(),memberRequest.getAliDeviceId());
        apiResponseDTO.initServicesResult(memberResponse);
        apiResponseDTO.setResponseData(gson.toJson(memberResponse.getResult()));
        logBase.info("updDeviceId_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

}
